package cuid.sc20.laboratory.controller;

import cuid.sc20.laboratory.annotation.Log;
import cuid.sc20.laboratory.common.AjaxResult;
import cuid.sc20.laboratory.common.BusinessType;
import cuid.sc20.laboratory.common.TableDataInfo;
import cuid.sc20.laboratory.domain.TbRole;
import cuid.sc20.laboratory.service.ITbRoleService;
import cuid.sc20.laboratory.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色Controller
 *
 * @author ruoyi
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/system/role")
public class TbRoleController extends BaseController
{
    @Autowired
    private ITbRoleService tbRoleService;

    /**
     * 查询角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbRole tbRole)
    {
        startPage();
        List<TbRole> list = tbRoleService.selectTbRoleList(tbRole);
        return getDataTable(list);
    }

    /**
     * 导出角色列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:export')")
    @Log(title = "角色", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRole tbRole)
    {
        List<TbRole> list = tbRoleService.selectTbRoleList(tbRole);
        ExcelUtil<TbRole> util = new ExcelUtil<TbRole>(TbRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    /**
     * 获取角色详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbRoleService.selectTbRoleById(id));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRole tbRole)
    {
        return toAjax(tbRoleService.insertTbRole(tbRole));
    }

    /**
     * 修改角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRole tbRole)
    {
        return toAjax(tbRoleService.updateTbRole(tbRole));
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbRoleService.deleteTbRoleByIds(ids));
    }
}
